const app = getApp()
var isLoadMore = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hideBottom: true,
    is_bottom: false,
    loadMoreData: "加载中...",
    bingPage: 1,
    isGetData: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getIntentData(options)
    var date = new Date();
    this.setData({
      refreshTime: date.toLocaleTimeString(),
      isGetData: false
    })

    this.connectNetForData()
  },
  connectNetForData() {
    var that = this
    if (!this.data.isGetData) {
      // 初始获取数据
      wx.showLoading({
        title: '拼命加载中...'
      })
    }
    wx.request({
      url: app.baseNet + 'roomEvaluate/queryRoomEvaluate',
      data: {
        sriRid: that.data.sriRid,
        bingPage: that.data.bingPage
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.statusCode == 200) {
          var msg = res.data.messcode
          if (msg == '001') {
            var roomEvaluateDtoList = res.data.roomEvaluateDtoList
            var roomList = []
            for (var j = 0; j < roomEvaluateDtoList.length; j++) {
              var envaluate = []
              var ddScore = roomEvaluateDtoList[j].user.score
              var user = roomEvaluateDtoList[j].user
              let scroeEvaluation = []
              for (var i = 0; i < 5; i++) {
                var src = ''
                if (i >= ddScore) {
                  src = '../../images/evaluate_unchecked.png'
                } else {
                  src = '../../images/evaluate_checked.png'
                }
                scroeEvaluation.push(src)
              }

              envaluate = {
                envaluate: scroeEvaluation,
                nickname: user.nickname,
                createTime: user.createTime,
                headPortrait: user.headPortrait,
                content: user.content,
                roomEvaluatePictureList: roomEvaluateDtoList[j].roomEvaluatePictureList
              }
              roomList.push(envaluate)
            }
            console.log(roomList)
            // that.setData({
            //   roomEvaluateDtoList: roomList
            // })

            if (isLoadMore) { //加载更多
              isLoadMore = false;
              if (roomEvaluateDtoList) {
                var data = that.data.roomEvaluateDtoList.concat(roomList)
                that.setData({
                  hideBottom: true,
                  loadMoreData: "加载中...",
                  roomEvaluateDtoList: data
                })

              } else {
                isLoadMore = false;
                that.setData({ //ds
                  hideBottom: false,
                  loadMoreData: "已经加载完毕",
                  isGetData: true,
                  is_bottom: true
                })
              }

            }

            if (!that.data.isGetData) { //初始获取数据
              wx.hideLoading()
              that.setData({
                messageCode: res.data.messageCode,
                roomEvaluateDtoList: roomList,
                allPages: res.data.pages,
                isGetData: true
              })
            }
          }

          that.setData({
            messcode: msg
          })

        } else {
          wx.showToast({
            title: '服务器罢工，小二正在抢修中~',
            duration: 2000
          })
        }

      }
    })
  },
  loadMore: function() {
    var that = this;
    isLoadMore = true;
    if (isLoadMore) {
      console.log("loadmore上拉加载更多");
      //当前页是最后一页
      if (that.data.bingPage == that.data.allPages) {
        isLoadMore = false;
        that.setData({ //ds
          hideBottom: false,
          loadMoreData: "已经加载完毕",
          isGetData: true,
          is_bottom: true
        })
        return
      } else {
        var currentPage = that.data.bingPage;
        currentPage = currentPage + 1;
        setTimeout(function() {
          that.setData({
            hideBottom: false,
            loadMoreData: "加载中...",
            is_bottom: false,
            isGetData: true,
            bingPage: currentPage
          })
          that.connectNetForData();
        }, 100)
      }
    }
  },

  /**
   * 传递数据
   */
  getIntentData(options) {
    var roomId = options.roomId
    var bingPage = 1;
    this.setData({
      sriRid: roomId,
      bingPage: bingPage
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})